﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace ATM
{
    public partial class Form1 : Form
    {
        public static string username;
        SqlConnection conn;
        public Form1()
        {
            InitializeComponent();
        }

        private void thôngTinNgânHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            thongtinbank ttnh = new thongtinbank();
            ttnh.Show();
        }

        private void quảnLýATMToolStripMenuItem_Click(object sender, EventArgs e)
        {
            thongtinatm ttatm = new thongtinatm();
            ttatm.Show();
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát hay không ?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Close();
                System.Windows.Forms.Application.Exit();
            }
        }

        private void quảnLýKháchHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            qlkhachhang qlkh = new qlkhachhang();
            qlkh.Show();
        }

        private void đăngKýKháchHàngToolStripMenuItem_Click(object sender, EventArgs e)
        {
            themkhachhang tkh = new themkhachhang();
            tkh.Show();
        }

        private void quảnLýTàiKhoảnToolStripMenuItem_Click(object sender, EventArgs e)
        {
            qltaikhoan qltk = new qltaikhoan();
            qltk.Show();
        }

        private void quảnLýGiaoDịchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            qlgiaodich qlgd = new qlgiaodich();
            qlgd.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel2.Text = DateTime.Now.ToString();
            toolStripStatusLabel1.Text = LoginAdmin.Username + " Online!";
        }

        private void kháchHàngToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            CRkhachhang rptkh = new CRkhachhang();
            rptkh.ShowDialog();
        }

        private void giaoDịchToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            CRgiaodich giaodich = new CRgiaodich();
            giaodich.ShowDialog();
        }

        private void tàiKhoảnToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            CRtaikhoan taikhoan = new CRtaikhoan();
            taikhoan.ShowDialog();
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void saoLưuCSDLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn sao lưu CSDL không?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string ngay = DateTime.Now.Day.ToString();
                string thang = DateTime.Now.Month.ToString();
                string nam = DateTime.Now.Year.ToString();
                string gio = DateTime.Now.Hour.ToString();
                string phut = DateTime.Now.Minute.ToString();
                //string giay = DateTime.Now.Second.ToString();
                try
                {

                    string path = "D://Thangxd" + ngay + "-" + thang + "-" + nam + "(" + gio + "h" + phut + ").bak";
                 
                    string sql = "BACKUP DATABASE [ATM_PM1] TO DISK = '" + path + "'";
                    SqlConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=ATM_PM1;Integrated Security=True");

                   
                    conn.Open();

                    SqlCommand cmd = new SqlCommand(sql, conn);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("Cơ sở dữ liệu đã được sao lưu ");

                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message, "Sao lưu CSDL");
                    return;
                }

            }
        }
        string path;
        public string Layduongdan()
        {

            OpenFileDialog _open = new OpenFileDialog();
            _open.Filter = "bak files(*.bak)|*.bak|Allfile (*.*)|*.*";
            if (_open.ShowDialog() == DialogResult.OK)
            {

                path = _open.FileName;
            }
            return path;
        }

        private void khôiPhụcCSDLToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn khôi phục CSDL?", "THÔNG BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                string path;
                string sqlRestore;
                try
                {
                    path = Layduongdan();
                    sqlRestore = "Use master Restore Database [ATM_PM1] from disk='" + path + "'";
                    SqlConnection conn = new SqlConnection(@"Data Source=(local);Initial Catalog=ATM_PM1;Integrated Security=True");
                   
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(sqlRestore, conn);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("Cơ sở dữ liệu đã được khôi phục ");

                }
                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message, "Restore Database");
                    return;
                }
            }
        }

        private void thoátToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có chắc chắn muốn thoát hay không ?", "Thoát", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Close();
                System.Windows.Forms.Application.Exit();
            }
        }

        
    }
}
